package edu.tsinghua.eea.powermanagement.gui;

import java.util.ArrayList;
import java.util.HashMap;

import javax.crypto.spec.OAEPParameterSpec;

import edu.tsinghua.eea.powermanagement.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PowerManager extends Activity {
	private static final int MENU_ITEM_OVERVIEW = Menu.FIRST;
	private static final int MENU_ITEM_APPLIANCE = Menu.FIRST + 1;
	private static final int MENU_ITEM_SUGGEST = Menu.FIRST + 2;
	private static final int MENU_ITEM_OPTION = Menu.FIRST + 3;
	private static final String MENU_NAME_TITLE = "itemTitle";
	private static final String MENU_NAME_TEXT = "itemText";

	private SimpleAdapter mMainMenuAdapter;
	private ArrayList<HashMap<String, Object>> mMainMenuItems;
	private ListView mMainMenu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);

		mMainMenu = (ListView) findViewById(R.id.main_menu_list);
		mMainMenuItems = new ArrayList<HashMap<String, Object>>();
		mMainMenuAdapter = new SimpleAdapter(this, mMainMenuItems,
				R.layout.main_menu_item,
				new String[] { MENU_NAME_TITLE, MENU_NAME_TEXT }, new int[] {
						R.id.menu_item_title, R.id.menu_item_text });
		mMainMenu.setAdapter(mMainMenuAdapter);
		initMainMenu();
		mMainMenu.setOnItemClickListener(mMenuListener);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_ITEM_OVERVIEW, 0, R.string.overview)
			.setIcon(android.R.drawable.ic_menu_myplaces);
		menu.add(0, MENU_ITEM_APPLIANCE, 1, R.string.appliance)
			.setIcon(android.R.drawable.ic_menu_agenda);
		menu.add(0, MENU_ITEM_SUGGEST, 2, R.string.suggest)
			.setIcon(android.R.drawable.ic_menu_manage);
		menu.add(0, MENU_ITEM_OPTION, 3, R.string.options)
			.setIcon(android.R.drawable.ic_menu_preferences);

		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case MENU_ITEM_OVERVIEW:
			actionOverviewActivity();
			break;
		case MENU_ITEM_APPLIANCE:
			actionStatusActivity();
			break;
		case MENU_ITEM_OPTION:
			actionOptionActivity();
			break;
		}
		return true;
	}
	
	private void initMainMenu(){
		HashMap<String, Object> map = new HashMap<String, Object>();
		Resources res = this.getResources();
		map.put(MENU_NAME_TITLE, res.getString(R.string.overview));
		map.put(MENU_NAME_TEXT, res.getString(R.string.overview_detail));
		mMainMenuItems.add(map);
		
		map = new HashMap<String, Object>();
		map.put(MENU_NAME_TITLE, res.getString(R.string.appliance));
		map.put(MENU_NAME_TEXT, res.getString(R.string.appliance_detail));
		mMainMenuItems.add(map);
		
		map = new HashMap<String, Object>();
		map.put(MENU_NAME_TITLE, res.getString(R.string.suggest));
		map.put(MENU_NAME_TEXT, res.getString(R.string.suggest_detail));
		mMainMenuItems.add(map);
		mMainMenuAdapter.notifyDataSetChanged();
	}
	
	private OnItemClickListener mMenuListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> l, View v, int position, long id){
			switch(position){
			case 0:
				actionOverviewActivity();
//				i = new Intent(v.getContext(), OverviewActivity.class);
//				v.getContext().startActivity(i);
				break;
			case 1:
				actionStatusActivity();
				break;
			case 2:
				break;
			}
		}
	};
	
	//******************
	// Actions
	//******
	private void actionOverviewActivity(){
		final Context context = this;
		Intent i = null;
		i = new Intent(context, OverviewActivity.class);
		context.startActivity(i);
	}
	
	private void actionStatusActivity(){
		final Context context = this;
		Intent i = null;
		i = new Intent(context, StatusActivity.class);
		context.startActivity(i);
	}
	
	private void actionOptionActivity(){
		final Context context = this;
		Intent i = null;
		i = new Intent(context, OptionActivity.class);
		context.startActivity(i);
	}
}